What is pino-std-serializers?
The pino-std-serializers package provides standard serializers for the Pino logging library. These serializers help in formatting various types of JavaScript objects and values into a more structured and readable format for logging purposes. This is particularly useful in applications where consistent log formats are necessary for debugging and monitoring.
What are pino-std-serializers's main functionalities?
req (HTTP request serializer)
Serializes HTTP request objects to log them in a structured format, including method, url, headers, and other relevant information.
const pino = require('pino');
const stdSerializers = require('pino-std-serializers');
const logger = pino({
serializers: {
req: stdSerializers.req
}
});
http.createServer((req, res) => {
logger.info({req}, 'request received');
res.end('hello world');
}).listen(3000);
res (HTTP response serializer)
Serializes HTTP response objects to capture and log status code, headers, and other response attributes.
const pino = require('pino');
const stdSerializers = require('pino-std-serializers');
const logger = pino({
serializers: {
res: stdSerializers.res
}
});
http.createServer((req, res) => {
res.on('finish', () => {
logger.info({res}, 'response sent');
});
res.end('hello world');
}).listen(3000);
err (Error serializer)
Provides a way to serialize error objects, including the message, stack trace, and other properties, which aids in debugging and error tracking.
const pino = require('pino');
const stdSerializers = require('pino-std-serializers');
const logger = pino({
serializers: {
err: stdSerializers.err
}
});
try {
throw new Error('Something went wrong');
} catch (err) {
logger.error({err}, 'error occurred');
}
Other packages similar to pino-std-serializers
morgan-json
While not a direct serializer library, morgan-json provides middleware to format HTTP request logs into JSON using Morgan, which can be seen as an alternative approach to logging HTTP requests compared to using pino-std-serializers with Pino.
pino-std-serializers
This module provides a set of standard object serializers for the
Pino logger.
Serializers
exports.err(error)
Serializes an Error
like object. Returns an object:
{
type: 'string',
message: 'string',
stack: 'string',
raw: Error
[...any additional Enumerable property the original Error had]
}
Any other extra properties, e.g. statusCode
, that have been attached to the
object will also be present on the serialized object.
If the error object has a cause
property, the cause
's message
and stack
will be appended to the top-level message
and stack
. All other parameters that belong to the error.cause
object will be omitted.
Example:
const serializer = require('pino-std-serializers').err;
const innerError = new Error("inner error");
innerError.isInner = true;
const outerError = new Error("outer error", { cause: innerError });
outerError.isInner = false;
const serialized = serializer(outerError);
### `exports.errWithCause(error)`
Serializes an `Error` like object, including any `error.cause`. Returns an object:
```js
{
type: 'string', // The name of the object's constructor.
message: 'string', // The supplied error message.
stack: 'string', // The stack when the error was generated.
cause?: Error, // If the original error had an error.cause, it will be serialized here
raw: Error // Non-enumerable, i.e. will not be in the output, original
// Error object. This is available for subsequent serializers
// to use.
[...any additional Enumerable property the original Error had]
}
Any other extra properties, e.g. statusCode
, that have been attached to the object will also be present on the serialized object.
Example:
const serializer = require('pino-std-serializers').errWithCause;
const innerError = new Error("inner error");
innerError.isInner = true;
const outerError = new Error("outer error", { cause: innerError });
outerError.isInner = false;
const serialized = serializer(outerError);
exports.mapHttpResponse(response)
Used internally by Pino for general response logging. Returns an object:
{
res: {}
}
Where res
is the response
as serialized by the standard response serializer.
exports.mapHttpRequest(request)
Used internall by Pino for general request logging. Returns an object:
{
req: {}
}
Where req
is the request
as serialized by the standard request serializer.
exports.req(request)
The default request
serializer. Returns an object:
{
id: 'string',
method: 'string',
url: 'string',
query: 'object',
params: 'object',
headers: Object,
remoteAddress: 'string',
remotePort: Number,
raw: Object
}
exports.res(response)
The default response
serializer. Returns an object:
{
statusCode: Number,
headers: Object,
raw: Object
}
exports.wrapErrorSerializer(customSerializer)
A utility method for wrapping the default error serializer. This allows
custom serializers to work with the already serialized object.
The customSerializer
accepts one parameter — the newly serialized error
object — and returns the new (or updated) error object.
exports.wrapRequestSerializer(customSerializer)
A utility method for wrapping the default request serializer. This allows
custom serializers to work with the already serialized object.
The customSerializer
accepts one parameter — the newly serialized request
object — and returns the new (or updated) request object.
exports.wrapResponseSerializer(customSerializer)
A utility method for wrapping the default response serializer. This allows
custom serializers to work with the already serialized object.
The customSerializer
accepts one parameter — the newly serialized response
object — and returns the new (or updated) response object.
License
MIT License